﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using org.swinchester.windowsevents.mongodb;
using org.swinchester.windowsevents.core;
using System.Configuration;
using System.IO;

namespace eventlog2mongodb
{
    public partial class EventLog2MongoDB : ServiceBase
    {
        private WindowsEventsToMongoDB subscriber;
        protected Thread serviceThread;

        public EventLog2MongoDB()
        {
            InitializeComponent();
        }

        private MongoDbConfig ConfigureMongo()
        {
            MongoDbConfig dbConfig = new MongoDbConfig
            {
                ConnectionString = ConfigurationManager.AppSettings["mongoDbConnectionString"].ToString(),
                Database = ConfigurationManager.AppSettings["mongoDbDatabase"].ToString(),
                ComputerEntities = ConfigurationManager.AppSettings["mongoDbMachineEntitiesName"].ToString(),
                EventEntities = ConfigurationManager.AppSettings["mongoDbEventsEntitiesName"].ToString()
            };
            return dbConfig;
        }

        private EventSubscriptionConfig ConfigureSubscriber()
        {
            EventSubscriptionConfig evtConfig = new EventSubscriptionConfig
            {
                EventLogName = ConfigurationManager.AppSettings["eventLogName"].ToString(),
                EventPathType = System.Diagnostics.Eventing.Reader.PathType.LogName,
                EventLogQuery = ReadQueryFromFile(ConfigurationManager.AppSettings["eventLogQuery"].ToString())
            };

            return evtConfig;
        }

        private EventSubscriptionConfig CatchupConfig()
        {
            EventSubscriptionConfig evtConfig = new EventSubscriptionConfig
            {
                EventLogName = ConfigurationManager.AppSettings["eventLogName"].ToString(),
                EventPathType = System.Diagnostics.Eventing.Reader.PathType.LogName,
                EventLogQuery = ReadQueryFromFile(ConfigurationManager.AppSettings["eventLogQueryCatchup"].ToString())
            };

            return evtConfig;
        }

        private string ReadQueryFromFile(string fileName)
        {
            StreamReader sr = null;
            string szQuery = "";
            try
            {
                sr = new StreamReader(fileName);
                szQuery = sr.ReadToEnd();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                sr.Dispose();
            }
            return szQuery;
        }

        protected override void OnStart(string[] args)
        {
            subscriber = new WindowsEventsToMongoDB();
            subscriber.dbConfig = ConfigureMongo();
            subscriber.CatchupConfig = CatchupConfig();
            subscriber.SubscriberConfig = ConfigureSubscriber();
            serviceThread = new Thread(new ThreadStart(subscriber.SubscribeToEvents));
            serviceThread.Start();
        }

        protected override void OnStop()
        {
        }

        public void DebugStart()
        {
            subscriber = new WindowsEventsToMongoDB();
            subscriber.dbConfig = ConfigureMongo();
            subscriber.CatchupConfig = CatchupConfig();
            subscriber.SubscriberConfig = ConfigureSubscriber();
            subscriber.SubscribeToEvents();
        }
    }
}
